package com.e3.basdoc.bank.dao;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import com.e3.basdoc.bank.entity.BankEntity;
import com.e3.basdoc.bank.model.BankModel;
import com.e3.basdoc.bank.model.BankQC;
import com.e3.core.base.SuperDAO;

public class BankDAO extends SuperDAO {
	
	private String querySQL = "SELECT t FROM BankEntity t";
	private String countSQL = "SELECT count(t) FROM BankEntity t";
	private String nativeQuerySQL = "SELECT t.id,t.code,t.name,t.short_name,t.region_id,t.address,t.parent_id,t.remark,(select count(n.id) from t_bank n where n.parent_id=t.id) as childrenSize FROM t_bank t";
	
	public List<BankModel> getBankChildren(long parentId){
		String querySQL = nativeQuerySQL + " WHERE t.parent_id=?";
		List<?> result = this.executeNativeQuery(querySQL,new Object[]{parentId});
		return resultToBankModel(result);
	}
	
	public BankEntity getBankByCode(String code) {
		String whSql = " WHERE t.code = ?";
		String sql = querySQL + whSql;
		List<BankEntity> entities = this.find(sql, code);
		return (entities == null || entities.size() == 0) ? null : entities.get(0);
	}

	public void deleteBankByIds(List<Long> ids) {
		if(ids == null||ids.isEmpty()){
			return;
		}
		
		Long[] idArray = new Long[ids.size()];
		ids.toArray(idArray);
		this.deleteAllBySingleField(BankEntity.class, "id", idArray);
	}
	
	private List<BankModel> resultToBankModel(List<?> result) {
		if(result!=null){
			List<BankModel> banks = new ArrayList<BankModel>(result.size());
			for (Object object : result) {
				Object[] values = (Object[])object;
				BankModel model = new BankModel();
				model.setId(((BigInteger)values[0]).longValue());
				model.setCode((String)values[1]);
				model.setName((String)values[2]);
				model.setShortName((String)values[3]);
				model.setRegionId(((BigInteger)values[4]).longValue());
				model.setAddress((String)values[5]);
				model.setParentId(((BigInteger)values[6]).longValue());
				model.setRemark((String)values[7]);
				model.setHasChildren(((BigInteger)values[8]).intValue()>0);
				banks.add(model);
			}
			return banks;
		}
		return null;
	}

	@SuppressWarnings("rawtypes")
	public List<Long> getChildrenIds(List<Long> bankIds) {
		if(bankIds==null||bankIds.isEmpty()){
			return null;
		}
		String querySQL = "SELECT t.id FROM t_bank t WHERE ";
		StringBuffer where = new StringBuffer();
		int length = bankIds.size();
		for (int i=0;i<length;i++) {
			long id = bankIds.get(i);
			if(i==length-1){
				where.append("t.parent_id=" + id);
			}
			else{
				where.append("t.parent_id=" + id+" OR ");
			}
		}
		
		String sql = querySQL + where.toString();
		List result = this.executeNativeQuery(sql ,null);
		if(result!=null){
			List<Long> ids = new ArrayList<Long>();
			for (Object object : result) {
				BigInteger bi = (BigInteger)object;
				ids.add(bi.longValue());
			}
			return ids;
		}
		return null;
	}
	
	@SuppressWarnings("rawtypes")
	public List<Long> getBankIdsByCondition(BankQC bankQC) {
		String sql = "SELECT t.id FROM BankEntity t";
		bankQC.setPrefix("t");
		String whereSQL = bankQC.getWhereSQL();
		if (whereSQL != null && whereSQL.length() > 0) {
			sql = sql + " WHERE " + whereSQL;
		}
		List result = executeHQLQuery(sql, null);
		if(result!=null){
			List<Long> ids = new ArrayList<Long>(result.size());
			for (Object object : result) {
				ids.add((Long)object);
			}
			return ids;
		}
		return null;
	}

	public List<BankModel> getAllBanks(){
		List result = this.executeNativeQuery(nativeQuerySQL,null);
		return resultToBankModel(result);
	}
	
	public long getBankSizeByCode(String code){
		String whSql = " WHERE t.code = ?";
		String sql = countSQL + whSql;
		return counter(sql,code);
	}
	
	public long getBankSizeByName(String name){
		String whSql = " WHERE t.name = ?";
		String sql = countSQL + whSql;
		return counter(sql,name);
	}


}
